<template>
  <div class="container">
    <Header v-bind:isBack="isBack" v-bind:isSea="isSea" @fn="getTitle"></Header>
    <!-- 活动详情部分 -->
    <div class="activity" v-if="status == 1">
      <div class="top-card">
        <div class="imgs">
          <img :src="detailObj.audition_activity_cover" alt />
          <div class="isOver" v-show="detailObj.type == 5">
            <img :src="activity" alt />
          </div>
        </div>
        <div class="title">
          <span class="fs-36">{{detailObj.audition_activity_name}}</span>
        </div>
        <div class="time fs-26">
          <div class="img">
            <img :src="clock" alt />
          </div>
          <span>{{detailObj.start_time}}</span>
        </div>
        <div class="vote fs-26">
          <div class="img">
            <img :src="clock" alt />
          </div>
          <span>{{detailObj.end_time}}</span>
        </div>
        <div class="rules">
          <div class="img">
            <img :src="rules" alt />
          </div>
          <span>{{detailObj.vote_rule}}</span>
        </div>
      </div>
      <div class="introduce">
        <div class="title fs-36">活动介绍：</div>
        <div class="content fs-32">{{detailObj.detail}}</div>
      </div>
      <!-- 报名按钮 -->
      <div
        class="btn-box"
        v-show="detailObj.type == 1 || detailObj.type == 2 && detailObj.is_enter != 1"
      >
        <div class="btn" @click="toSignUp()">
          <span>我要报名</span>
        </div>
      </div>
      <!-- 已经报过名的显示已报名 -->
      <div class="btn-box active" v-show="detailObj.is_enter == 1">
        <div class="btn">
          <span>已报名</span>
        </div>
      </div>
      <!-- 1=报名开始前 2=报名中 3=报名结束到投票开始时间端 4=投票中 5=投票结束 -->
      <!-- 报名已截止 -->
      <div class="btn-box active" v-show="detailObj.is_enter != 1 && detailObj.type == 3 || detailObj.type == 4 || detailObj.type == 5">
        <div class="btn">
          <span>报名已截止</span>
        </div>
      </div>
    </div>
    <!-- 参赛者部分 -->
    <div class="match" v-show="status == 2">
      <div class="list">
        <div class="match-person">
          <div class="title fs-28">参赛人数</div>
          <div class="num">{{topData.enter_num}}</div>
        </div>
        <div class="match-vote">
          <div class="title fs-28">累计投票</div>
          <div class="num">{{topData.vote_num}}</div>
        </div>
        <div class="visit-num">
          <div class="title fs-28">访问量</div>
          <div class="num">{{topData.run_num}}</div>
        </div>
      </div>
      <div class="search-btn">
        <input class="btn" placeholder="搜索编号、人数" @focus="changeContent" v-model.trim="keywords" />
        <img class="delete-icon" v-show="!isInput" @click="clearContent" :src="closePic" alt />
        <img class="search-icon" v-show="isInput" :src="search" alt />
      </div>
      <div class="match-list" v-if="contestentList && contestentList.length != 0">
        <div
          class="ml-item"
          v-for="(item,index) in contestentList"
          :key="index"
          @click="toMatchDetail(item.id)"
        >
          <div class="img">
            <img :src="item.user_avator" alt />
            <span class="mine fs-28" v-show="item.self == 1">我</span>
          </div>
          <div class="center">
            <div class="name fs-28">{{item.number}}.{{item.name}}</div>
            <div class="flower fs-24">{{item.vote_num}}鲜花</div>
          </div>
          <div class="bottom">
            <div class="area fs-24">{{item.game_area}}</div>
            <div class="btn fs-28" @click.stop="showModel(item.id)">送花</div>
            <!-- <div class="btns fs-28">送花</div> -->
          </div>
        </div>
      </div>
      <div class="noMes" v-else>没有匹配的参赛选手奥</div>
    </div>
    <!-- 送花模态框 -->
    <div class="model" v-show="isShowModel" @touchmove.prevent @click.stop="closeFlowerModel">
      <div class="main-mes" @click.stop="mainValue">
        <div class="flower">
          <input
            type="number"
            oninput="value=value.replace(/[^\d]/g,'')"
            placeholder="请输入送花数量"
            v-model="sendNum"
          />
          <span class="fs-32">支</span>
        </div>
        <div class="flower-num fs-32">
          您当前拥有
          <span>{{flowerMes.num}}</span>
          支鲜花
        </div>
        <div class="flower-btn" v-show="!isFree">
          <div class="pay fs-32" @click="buyFlower()">购买鲜花</div>
          <div class="send fs-32" v-show="flowerMes.num != 0" @click="sendFolwer()">送花</div>
          <div class="send active fs-32" v-show="flowerMes.num == 0">送花</div>
        </div>
        <div class="flower-btn-s fs-32" v-show="isFree">送花</div>
        <div class="close-btn" @click="closeFlowerModel">
          <img :src="closeModel" alt />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 节流函数
const delay = (function() {
  let timer = 0;
  return function(callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
})();
export default {
  name: "sea-activity",
  data() {
    return {
      title: "",
      isBack: true,
      isSea: true,
      isEnd: false, //活动是否已经结束
      isFree: false, //不是免费
      status: 1, //1表示当前点击的活动详情，2表示参赛者
      isInput: true, //搜索框的搜索按钮
      id: 1, //海选活动id
      enter_activity_id: null, //当前点击的参赛者id
      keywords: "", // 海选参赛者的搜索关键字
      sendNum: null, //送花的数量
      isShowModel: false, //送花model的显示和隐藏
      clock: require("@/assets/index/clock.png"),
      rules: require("@/assets/index/rules.png"),
      search: require("@/assets/index/search-sea.png"),
      detailObj: {}, //活动详情
      activity: require("@/assets/index/activity.png"),
      closePic: require("@/assets/index/close.png"),
      closeModel: require("@/assets/index/close-btn.png"),
      contestentList: [], //参赛者列表数据
      topData: {}, //参赛者列表头部数据
      flowerMes: {
        num: 8
      }
    };
  },
  watch: {
    keywords() {
      delay(() => {
        this.isInput = false;
        this.getContestent();
        if (this.keywords == "") {
          this.isInput = true;
        }
      }, 0);
    },
    sendNum: function() {
      if (this.sendNum.charAt(0) == 0) {
        this.sendNum = null;
      }
    }
  },
  components: {},
  methods: {
    // 获取我所拥有的鲜花数量
    getFlowerNumber() {
      const url =
        this.$host1 +
        `home/audition/flowerNum?audition_activity_vote_time_id=` +
        this.id;
      this.$axios
        .get(url)
        .then(res => {
          // console.log(res.data.data, "---res.data.data--");
          if (res.data.code == 200) {
            this.flowerMes.num = res.data.data.flower_num;
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 去填写报名信息页面
    toSignUp() {
      this.$router.push({
        path: "/index/sign-up",
        query: { id: this.id }
      });
    },
    // 去参赛者详情页面
    toMatchDetail(id) {
      this.$router.push({
        path: "/index/match-detail",
        query: { id: id, activityId: this.id }
      });
    },
    // 购买鲜花
    buyFlower() {
      this.$router.push({
        path: "/index/buy-flower",
        query: { id: this.id, userId: this.enter_activity_id }
      });
    },
    // 送花
    sendFolwer() {
      if (this.sendNum == null || this.sendNum == 0) {
        this.AlertWin("请输入正确的鲜花数量");
      } else if (this.sendNum > this.flowerMes.num) {
        this.AlertWin("您所拥有的鲜花数量不足，快去购买吧");
      } else {
        this.sendFlowerFn();
      }
    },
    // 送花接口
    sendFlowerFn() {
      const url = this.$host1 + `home/audition/sendFlower`;
      var params = {
        audition_activity_vote_time_id: this.id,
        enter_activity_id: this.enter_activity_id,
        send_flower_num: this.sendNum
      };
      this.$axios
        .post(url, params)
        .then(res => {
          console.log(res.data.data, "---res.data.data--");
          if (res.data.code == 200) {
            this.sendNum = null;
            this.closeFlowerModel();
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 解决关掉模态框问题
    mainValue() {},
    // 关掉模态框
    closeFlowerModel() {
      this.isShowModel = false;
      this.getContestent(); // 获取该详情对应的参赛者列表
      this.getFlowerNumber(); //获取我所拥有的鲜花数量
    },
    // 打开送花模态框
    showModel(id) {
      this.isShowModel = true;
      this.enter_activity_id = id;
    },
    // 清空搜索框内容
    clearContent() {
      this.keywords = "";
    },
    changeContent() {
      // input获取焦点
    },
    // 获取当前点击的头部标题
    getTitle(e) {
      this.status = e;
    },
    // 获取海选活动详情接口
    getActivityDetail() {
      const url =
        this.$host1 +
        `home/audition/auditionActivityMsg?audition_activity_vote_time_id=` +
        this.id;
      this.$axios
        .get(url)
        .then(res => {
          // console.log(res.data, "---res.data--");
          if (res.data.code == 200) {
            this.detailObj = res.data.data;
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 获取该详情对应的参赛者列表
    getContestent() {
      const url =
        this.$host1 +
        `home/audition/enterAuditionActivityList?audition_activity_vote_time_id=` +
        this.id +
        `&keywords=` +
        this.keywords;
      this.$axios
        .get(url)
        .then(res => {
          console.log(res.data, "---res.data--");
          if (res.data.code == 200) {
            this.topData = res.data.data.top;
            this.contestentList = res.data.data.list;
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    }
  },
  created() {
    this.id = this.$route.query.id;
    this.getActivityDetail(); // 获取海选活动详情接口
    this.getContestent(); // 获取该详情对应的参赛者列表
    this.getFlowerNumber(); //获取我所拥有的鲜花数量
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  padding-top: 1rem;
  // 活动详情的样式设置
  .activity {
    .top-card {
      padding: 0.24rem 0.25rem 0 0.23rem;
      box-sizing: border-box;
      background-color: #fff;
      .imgs {
        position: relative;
        width: 7.02rem;
        height: 4rem;
        margin-bottom: 0.39rem;
        border-radius: 0.18rem;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
        .isOver {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
          background-color: rgba(0, 0, 0, 0.6);
          img {
            width: 2.22rem;
            height: 2.22rem;
            position: absolute;
            left: 50%;
            top: 25%;
            transform: translate(-50%);
          }
        }
      }
      .title {
        margin-bottom: 0.3rem;
        span {
          font-weight: bold;
          color: rgba(68, 68, 68, 1);
          line-height: 0.48rem;
        }
      }
      .time,
      .vote,
      .rules {
        display: flex;
        .img {
          width: 0.34rem;
          height: 0.34rem;
          margin-top: 0.1rem;
          margin-right: 0.11rem;
          img {
            width: 100%;
          }
        }
        span {
          font-weight: 500;
          color: rgba(153, 153, 153, 1);
          line-height: 0.48rem;
        }
      }
      .rules {
        padding-bottom: 0.34rem;
        box-sizing: border-box;
        border-bottom: 0.01rem solid #e9e9e9;
      }
    }
    // 活动介绍部分
    .introduce {
      padding: 0.35rem 0.24rem 1.73rem;
      box-sizing: border-box;
      background-color: #fff;
      .title {
        font-weight: bold;
        color: rgba(68, 68, 68, 1);
        line-height: 0.48rem;
        margin-bottom: 0.25rem;
      }
      .content {
        color: #666666;
      }
    }
    //我要报名按钮
    .btn-box {
      position: fixed;
      left: 0;
      bottom: 0.3rem;
      width: 100%;
      height: 0.9rem;
      background-color: #fff;
      .btn {
        width: 7.02rem;
        height: 0.9rem;
        background: rgba(84, 4, 121, 1);
        border-radius: 0.45rem;
        text-align: center;
        line-height: 0.9rem;
        margin: 0 auto;
        span {
          font-weight: bold;
          color: rgba(255, 255, 255, 1);
          line-height: 0.36rem;
        }
      }
    }
    .active {
      .btn {
        background-color: #dddddd;
        span {
          color: #444444;
        }
      }
    }
  }

  // 参赛者的样式
  .match {
    width: 100%;
    .list {
      padding-top: 1rem;
      box-sizing: border-box;
      background: linear-gradient(to right, #680dae, #9529c6);
      display: flex;
      justify-content: space-around;
      .match-person,
      .match-vote,
      .visit-num {
        text-align: center;
        .title,
        .num {
          font-weight: 500;
          color: rgba(255, 255, 255, 1);
          line-height: 0.32rem;
          margin-bottom: 0.2rem;
        }
      }
    }
    .search-btn {
      position: relative;
      width: 100%;
      height: 1.02rem;
      padding-top: 0.34rem;
      background-color: #fff;
      input {
        display: block;
        width: 7.01rem;
        height: 0.58rem;
        margin: 0 auto;
        border: 0.01rem solid rgba(153, 153, 153, 1);
        border-radius: 0.29rem;
        padding: 0 0.25rem;
        box-sizing: border-box;
      }
      input::-webkit-input-placeholder {
        text-align: center;
        font-size: 0.28rem;
        color: #999999;
      }
      img {
        position: absolute;
        width: 0.3rem;
        height: 0.3rem;
        left: 28%;
        top: 0.47rem;
      }
      .delete-icon {
        width: 0.2rem;
        height: 0.2rem;
        left: 90%;
        top: 0.53rem;
      }
    }
    .match-list {
      padding: 0.24rem 0.1rem 0.22rem 0.24rem;
      box-sizing: border-box;
      display: flex;
      flex-wrap: wrap;
      background-color: #f5f5f5;
      .ml-item {
        width: 3.42rem;
        height: 6rem;
        margin-right: 0.16rem;
        margin-bottom: 0.24rem;
        background-color: #fff;
        .img {
          position: relative;
          width: 100%;
          height: 4.4rem;
          margin-bottom: 0.2rem;
          border-radius: 0.06rem;
          overflow: hidden;
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
          .mine {
            position: absolute;
            left: 0;
            top: 0;
            width: 0.48rem;
            height: 0.48rem;
            background: rgba(84, 4, 120, 1);
            font-weight: bold;
            color: rgba(255, 255, 255, 1);
            text-align: center;
            line-height: 0.48rem;
            border-radius: 0.06rem;
          }
        }
        .center {
          display: flex;
          justify-content: space-between;
          padding: 0 0.18rem;
          box-sizing: border-box;
          margin-bottom: 0.25rem;
          .name {
            width: 1.6rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-weight: 500;
            color: rgba(68, 68, 68, 1);
          }
          .flower {
            font-weight: bold;
            color: rgba(244, 67, 73, 1);
          }
        }
        .bottom {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 0 0.18rem;
          box-sizing: border-box;
          .area {
            font-weight: 500;
            color: rgba(68, 68, 68, 1);
          }
          .btn,
          .btns {
            width: 1.12rem;
            height: 0.46rem;
            line-height: 0.5rem;
            border-radius: 0.06rem;
            background: rgba(244, 67, 73, 1);
            text-align: center;
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
          }
          .btns {
            background: rgba(221, 221, 221, 1);
            color: #444;
          }
        }
      }
    }

    .noMes {
      text-align: center;
      margin-top: 0.5rem;
    }
  }
  // 模态框样式设置
  .model {
    width: 100%;
    height: 100vh;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    .main-mes {
      width: 6.77rem;
      height: 4.6rem;
      background-color: #fff;
      position: absolute;
      left: 50%;
      top: 25%;
      transform: translate(-50%);
      padding: 0.7rem 0.4rem 0;
      border-radius: 0.08rem;
      box-sizing: border-box;
      .flower {
        position: relative;
        margin-bottom: 0.43rem;
        input {
          width: 5.98rem;
          height: 0.88rem;
          border: 0.01rem solid rgba(153, 153, 153, 1);
          border-radius: 0.06rem;
          text-indent: 0.26rem;
        }
        input::-webkit-input-placeholder {
          font-size: 0.32rem;
          font-weight: 500;
          color: rgba(153, 153, 153, 1);
        }
        span {
          position: absolute;
          right: 0.26rem;
          top: 0.25rem;
          font-weight: 500;
          color: rgba(68, 68, 68, 1);
        }
      }

      .flower-num {
        font-weight: 500;
        color: rgba(68, 68, 68, 1);
        line-height: 0.32rem;
        margin-bottom: 0.79rem;
        span {
          color: #f44349;
        }
      }

      .flower-btn {
        display: flex;
        justify-content: space-between;
        .pay,
        .send {
          width: 2.84rem;
          height: 0.84rem;
          background: rgba(244, 67, 73, 1);
          border-radius: 0.06rem;
          text-align: center;
          line-height: 0.84rem;
          font-weight: bold;
          color: rgba(255, 255, 255, 1);
        }
        .active {
          background: rgba(245, 245, 245, 1);
          color: #444;
        }
      }
      .flower-btn-s {
        width: 100%;
        height: 0.84rem;
        background: rgba(244, 67, 73, 1);
        border-radius: 0.06rem;
        text-align: center;
        line-height: 0.84rem;
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
      }
      .close-btn {
        width: 0.97rem;
        height: 0.97rem;
        position: absolute;
        bottom: -1.72rem;
        left: 50%;
        transform: translate(-50%);
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>
